package com.weareint.xxljob.enable;

import com.weareint.xxljob.XxlJobConfig;
import com.weareint.xxljob.dynamic.*;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author johnny
 * @create 2021-07-02 4:54 下午
 */
@Configuration
@ConditionalOnClass(XxlJobSpringExecutor.class)
public class EnableScheduleXxlJobAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public XxlJobConfig xxlJobConfig() {
        return new XxlJobConfig();
    }

    @Bean
    @ConditionalOnMissingBean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


    @Bean
    @ConditionalOnBean(RestTemplate.class)
    public XxlJobOperators xxlJobOperators(RestTemplate restTemplate) {
        return new XxlJobOperators(restTemplate);
    }

    @Bean
    @ConditionalOnBean(XxlJobOperators.class)
    public XxlJobDynamicRegister xxlJobDynamicRegister(XxlJobOperators xxlJobOperators) {
        return new XxlJobDynamicRegister(xxlJobOperators);
    }

    @Bean
    @ConditionalOnBean(XxlJobOperators.class)
    public InitJobGroup initJobGroup(XxlJobOperators xxlJobOperators) {
        return new InitJobGroup(xxlJobOperators);
    }

    @Bean
    @ConditionalOnBean({IDynamicXxlJobRegisterTask.class, XxlJobDynamicRegister.class})
    public GlobalDynamicXxlJobRegistrar globalDynamicRegistrar(
            XxlJobDynamicRegister xxlJobDynamicRegister,
            List<IDynamicXxlJobRegisterTask> dynamicRegisterTasks) {
        return new GlobalDynamicXxlJobRegistrar(xxlJobDynamicRegister, dynamicRegisterTasks);
    }


}
